package dao.JPA;

import javax.persistence.EntityManager;

import dao.JPAFactoryDAO;
import bean.Genero;
import interfaces.GeneroInt;

/**
 * 
 * @author Daniel Rodrigues
 */
public class JPAGeneroDAO implements GeneroInt {

	@Override
	public void inserir(Genero gen) {
		EntityManager em = JPAFactoryDAO.getInstance().getFactory()
				.createEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(gen);
			em.getTransaction().commit();
		} catch (Exception e) {
			System.err.println(e.getMessage());
		} finally {
			em.close();
		}
	}

	@Override
	public void alterar(Genero gen) {
		EntityManager em = JPAFactoryDAO.getInstance().getFactory()
				.createEntityManager();
		try {
			em.getTransaction().begin();
			em.merge(gen);
			em.getTransaction().commit();
		} catch (Exception e) {
			System.err.println(e.getMessage());
		} finally {
			em.close();
		}

	}

	@Override
	public void deletar(Genero gen) {
		EntityManager em = JPAFactoryDAO.getInstance().getFactory()
				.createEntityManager();
		try {
			em.remove(gen);
		} catch (Exception e) {
			System.err.println(e.getMessage());
		} finally {
			em.close();
		}

	}

	@Override
	public Genero consultar(long id) {
		EntityManager em = JPAFactoryDAO.getInstance().getFactory()
				.createEntityManager();
		Genero gen = null;
		try {
			gen = (Genero) em.find(Genero.class, id);
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
		em.close();
		return gen;
	}

	@Override
	public Genero consultarGeneroPorNome(String nome) {
		// TODO Auto-generated method stub
		return null;
	}

}
